
#include <QAbstractEventDispatcher>
#include <QDebug>
#include "globalhotkey.h"
#include "globaldefine.h"


GlobalHotkey::GlobalHotkey(int &argc, char **argv):QApplication(argc, argv)
{
    m_atom = REG_HK_ID;
    m_hk = REG_HK;
}

GlobalHotkey::~GlobalHotkey()
{

}

bool GlobalHotkey::registerHotKey(StockInfo *window)
{
    bool reg = false;
    while(m_atom++ < 10000 && !reg)
    {
        qDebug() << "registerHotKey says: m_atom = "<<m_atom;
        reg = RegisterHotKey(window->winId(), m_atom, 0, m_hk);
    }

    if(!reg)
    {
        return REG_HK_ERR;
    }

    QAbstractEventDispatcher *evtdis = QAbstractEventDispatcher::instance();

    if (evtdis != NULL)
    {
        return REG_HK_ERR;
    }

    return REG_HK_SUC;
}

void GlobalHotkey::unregisterHotKey(StockInfo *window)
{
    UnregisterHotKey(window->winId(), m_atom);
}

bool GlobalHotkey::winEventFilter(MSG *msg, long *result)
{

    if (msg->message == WM_HOTKEY)
    {

        if(msg->wParam = m_atom)
        {
            qDebug() << "winEventFilter says: Hotkey pressed";
            emit signalPressedHotkey(m_atom);

            return true;
        }
        else
        {
            qDebug() << "winEventFilter says: unknown HotKey pressed";
        }
    }

    return false;
}
